package tech.aistar.prj.controller;

import tech.aistar.prj.model.dto.VideoDTO;
import tech.aistar.prj.model.pojo.User;
import tech.aistar.prj.model.pojo.Video;
import tech.aistar.prj.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/video/indexView")
public class IndexViewController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        List<VideoDTO> list = (List<VideoDTO>) req.getAttribute("videoList");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");
        out.println("<head>");
        out.println("<meta charset='UTF-8'>");
        out.println("<meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.println("<title>bootstrap是一款响应式的UI[User-Interface]框架</title>");
        out.println("<!-- 引入bootstrap.css - 开发环境/bootstrap.min.css - 生产环境-->");
        out.println("<link rel='stylesheet' href='/servlet/plugins/bootstrap-3.4.1-dist/css/bootstrap.css'>");
        out.println("<link rel='stylesheet' href='/servlet/css/user/login.css'>");
        out.println("<link rel='stylesheet' href='/servlet/css/video/index.css'>");
//        out.println("<link rel='stylesheet' href='/servlet/js/video/video.js'>");
        out.println("</head>");
        out.println("<body>");
        out.println("<div class='container-fluid'>");
//        查询条件
        out.println("<div class='row'>");
        out.println("<div class='input-group col-lg-3'>");
//        进行数据回显
        Map<String,String> dataMap = (Map<String, String>) req.getAttribute("dataMap");
        out.println("<span class='input-group-addon' id='basic-addon1'>用户名");out.println("</span>");
        out.println("<input type='text' name='username' value='"+dataMap.get("username")+"' class='form-control' placeholder='用户名' aria-describedby='basic-addon1'>");
        out.println("</div>");
        out.println("<div class='input-group col-lg-3'>");
        out.println("<span class='input-group-addon' id='basic-addon1'>评分");out.println("</span>");
        out.println("<input type='text' name='score'value='"+dataMap.get("score")+"' class='form-control' placeholder='大于等于' aria-describedby='basic-addon1'>");
        out.println("</div>");
        out.println("<div class='input-group col-lg-3'>");
        out.println("<span class='input-group-addon' id='basic-addon1'>点赞数");out.println("</span>");
        out.println("<input type='text' name='likes' value='"+dataMap.get("likes")+"' class='form-control' placeholder='大于等于' aria-describedby='basic-addon1'>");
        out.println("</div>");
        out.println("<div class='input-group col-lg-3'>");
        out.println("<span class='input-group-addon' id='basic-addon1'>标题");out.println("</span>");
        out.println("<input type='text' name='title' value='"+dataMap.get("title")+"' class='form-control' placeholder='标题' aria-describedby='basic-addon1'>");
        out.println("</div>");
        out.println("</div>");
        out.println("   <button id='search'>搜索</button>");



        out.println("<div class='row'>");
        out.println("<div class='table-responsive'>");
        out.println("<table class='table'>");
        out.println("<caption>videoInfo")
        ;out.println("</caption>");
        out.println("<thead>");
        out.println("<tr>");

        out.println("<th>");out.println("<input type='checkbox'>");out.println("</th>");
        out.println("<th style='visibility:hidden'>id");out.println("</th>");
        out.println("<th>title");out.println("</th>");
        out.println("<th>video_url");out.println("</th>");
        out.println("<th>user_id");out.println("</th>");
        out.println("<th>create_date");out.println("</th>");
        out.println("<th>likes&nbsp;&nbsp;<span id='rise' style='cursor: pointer;' class='text-warning glyphicon glyphicon-sort-by-attributes'>" +
                "</span>&nbsp;&nbsp;<span id='desc' style='cursor: pointer;' class='text-info glyphicon glyphicon-sort-by-attributes-alt'></span>");out.println("</th>");
        out.println("<th>score");out.println("</th>");
        out.println("<th>username");out.println("</th>");
        out.println("</tr>");
        out.println("</thead>");
        out.println("<tbody id='content'>");
        for (int i = 0; i < list.size(); i++) {
            out.println("<tr>");
            out.println("<td><input type='checkbox'></td>");
            out.println("<td style='visibility:hidden'>"+(i+1)+"</td>");
            out.println("<td>"+list.get(i).getVideo().getTitle());out.println("</td>");
            out.println("<td><img src='/servlet/imgs/"+list.get(i).getVideo().getVideoUrl());out.println("'></td>");
            out.println("<td>"+list.get(i).getVideo().getUserId());out.println("</td>");
            out.println("<td>"+ DateUtil.format(list.get(i).getVideo().getCreateDate(),"yyyy-MM-dd HH:mm:ss"));out.println("</td>");
            out.println("<td>"+list.get(i).getVideo().getLikes());out.println("</td>");
            out.println("<td>"+list.get(i).getVideo().getScore());out.println("</td>");
            out.println("<td>"+list.get(i).getUsername());out.println("</td>");
            if (null!=user && user.getPower()==0){
                out.println("<td><a href='/servlet/video/index?delId="+list.get(i).getVideo().getId()+"' class='btn btn-default' role='button'>删除</a></td>");
            }
            out.println("</tr>");
        }

        out.println("<input type='text' name='titleHid' value='"+dataMap.get("title")+"' style='display: none;'>");
        out.println("<input type='text' name='scoreHid' value='"+dataMap.get("score")+"' style='display: none;'>");
        out.println("<input type='text' name='likesHid' value='"+dataMap.get("likes")+"' style='display: none;'>");
        out.println("<input type='text' name='usernameHid' value='"+dataMap.get("username")+"' style='display: none;'>");


        out.println("</tbody>");
        out.println("</table>");
        out.println("</div>");
        out.println("</div>");
        out.println("</div>");


        out.println("<script src='/servlet/plugins/jquery.min.js'>");out.println("</script>");

        out.println("<!-- 引入bootstrap.js/bootstrap.min.js,bootstrap依赖于jquery框架 -->");
        out.println("<script src='/servlet/plugins/bootstrap-3.4.1-dist/js/bootstrap.js'>");out.println("</script>");
        out.println("<script src='/servlet/js/video/video.js'>");out.println("</script>");

        out.println("</body>");
        out.println("</html>");
    }
}
